package com.globalresources.ui.struts.action.document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.globalresources.business.inface.IDocumentService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.commons.StringHelper;
import com.globalresources.commons.vo.UserAndDocumentWrapper;
import com.globalresources.po.Document;
import com.globalresources.po.User;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 13, 2009 9:09:20 AM
 */
public class HospitalDocumentAction extends AbstractAction implements ServletRequestAware{
	private HttpServletRequest request;
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PagerService pagerService;
	private IDocumentService documentService;
	private IUserService userService;
	
	private List<UserAndDocumentWrapper> userAndDocumentList;
	private Pager userAndDocumentPager;
	private String userAndDocumentPagerMethod;
	private String userAndDocumentTotalRow;
	private String userAndDocumentCurrentPage; 
	
	private String username;
	private List<Document> documents;
	private Pager documentsPager;
	private String documentsMethod;
	private String documentsTotalRow;
	private String documentsCurrentPage; 
	
	
	private User user;
	private String order_by_flag;
	private List<UserAndDocumentWrapper> rankUserAndDocuementList;
	
	private String show_label;
	private List<Map<String, String>> otherLinks;
	
	
	
	/**
	 * 报表汇总
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String listHospitalDocumentSum(){
		String user_name = getCurrentUerName();		
		if (StringHelper.isNotEmpty(user_name)) {
			List<User> owners = userService.getUser(user_name);
			if (null != owners && owners.size() > 0) {
				User owner = owners.get(0);
				if (!StringHelper.isNotEmpty(order_by_flag))  order_by_flag = "username";
				List<UserAndDocumentWrapper> tmplist = documentService.getDocumentSum(owner.getDetailUser().getHospital().getId(), order_by_flag);
				userAndDocumentPager = pagerService.getPager(userAndDocumentCurrentPage, userAndDocumentPagerMethod, tmplist.size());
				setUserAndDocumentTotalRow(String.valueOf(tmplist.size()));
				setUserAndDocumentCurrentPage(String.valueOf(userAndDocumentPager.getCurrentPage()));
				int toEnd = tmplist.size();
				if (tmplist.size() > (userAndDocumentPager.getStartRow() + userAndDocumentPager.getPageSize())) {
					toEnd = userAndDocumentPager.getStartRow() + userAndDocumentPager.getPageSize();
				}
				userAndDocumentList = tmplist.subList(userAndDocumentPager.getStartRow(), toEnd);
			}
		}
		return SUCCESS;
	}
	
	/**
	 * <p>个人文档列表
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String listDocumentDetail(){
		List<User> owners = userService.getUser(username);
		if(null != owners && owners.size() > 0){
			user = owners.get(0);
			int _totalRow = documentService.countDocuments(user.getDetailUser().getId(), null, null);
			documentsPager = pagerService.getPager(documentsCurrentPage, documentsMethod, _totalRow);
			setDocumentsTotalRow(String.valueOf(_totalRow));
			setDocumentsCurrentPage(String.valueOf(documentsPager.getCurrentPage()));
			documents = documentService.getDocuments(user.getDetailUser().getId(), null, null);
		}
		
		return SUCCESS;
	}
	
	/**
	 * <P>排行榜
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String listRankDocumentSum(){
		String user_name = getCurrentUerName();
		if (StringHelper.isNotEmpty(user_name)) {
			List<User> owners = userService.getUser(user_name);
			if (null != owners && owners.size() > 0) {
				User owner = owners.get(0);
				if (!StringHelper.isNotEmpty(order_by_flag))  order_by_flag = "unsign_documnet_count";
				List<UserAndDocumentWrapper> tmplist = documentService.getDocumentSum(owner.getDetailUser().getHospital().getId(), order_by_flag);
				int toEnd = 10;
				if (tmplist.size() < 10) {
					toEnd = tmplist.size();
				}					
				rankUserAndDocuementList = tmplist.subList(0, toEnd);
			}
		}
		initShow(order_by_flag);
		order_by_flag = "unsign_documnet_count";
		return SUCCESS;
	}
	
	/**
	 * 
	*@author aaron.lu(luzhiyuan)
	*@param order
	 */
	private void initShow(String order){
		if (order.equals("unsign_documnet_count")) {
			setShow_label("UnSign");
			Map<String, String> record1 = new HashMap<String, String>();
			otherLinks = new ArrayList<Map<String,String>>();
			record1.put("name", "Rank Signed Documents");
			record1.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=signed_document_count\">Rank Signed Documents</a>");
			otherLinks.add(record1);
			Map<String, String> record2 = new HashMap<String, String>();
			record2.put("name", "Rank All Documents");
			record2.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=all_documnet_count\">Rank All Documents</a>");
			otherLinks.add(record2);
		}else if (order.equals("signed_document_count")) {
			setShow_label("Sign");
			otherLinks = new ArrayList<Map<String,String>>();
			Map<String, String> record1 = new HashMap<String, String>();
			record1.put("name", "Rank UnSign Documents");
			record1.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=unsign_documnet_count\">Rank UnSign Documents</a>");
			otherLinks.add(record1);
			Map<String, String> record2 = new HashMap<String, String>();
			record2.put("name", "Rank All Documents");
			record2.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=all_documnet_count\">Rank All Documents</a>");
			otherLinks.add(record2);
		}else if (order.equals("all_documnet_count")) {
			setShow_label("All");
			otherLinks = new ArrayList<Map<String,String>>();
			Map<String, String> record1 = new HashMap<String, String>();
			record1.put("name", "Rank UnSign Documents");
			record1.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=unsign_documnet_count\">Rank UnSign Documents</a>");
			otherLinks.add(record1);
			Map<String, String> record2 = new HashMap<String, String>();
			record2.put("name", "Rank Signed Documents");
			record2.put("link", "<a href=\"listRankDocumentSum.action?order_by_flag=signed_document_count\">Rank Signed Documents</a>");
			otherLinks.add(record2);
		}
	}
	
	/**
	 * <p>获取当前用户名字
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String getCurrentUerName(){
		String name = "";
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails userDetails = (UserDetails)obj;
			name = userDetails.getUsername();
		}
		return name;
	}
	
	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public IDocumentService getDocumentService() {
		return documentService;
	}

	public void setDocumentService(IDocumentService documentService) {
		this.documentService = documentService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public List<UserAndDocumentWrapper> getUserAndDocumentList() {
		return userAndDocumentList;
	}

	public void setUserAndDocumentList(List<UserAndDocumentWrapper> userAndDocumentList) {
		this.userAndDocumentList = userAndDocumentList;
	}

	public Pager getUserAndDocumentPager() {
		return userAndDocumentPager;
	}

	public void setUserAndDocumentPager(Pager userAndDocumentPager) {
		this.userAndDocumentPager = userAndDocumentPager;
	}

	public String getUserAndDocumentPagerMethod() {
		return userAndDocumentPagerMethod;
	}

	public void setUserAndDocumentPagerMethod(String userAndDocumentPagerMethod) {
		this.userAndDocumentPagerMethod = userAndDocumentPagerMethod;
	}

	public String getUserAndDocumentTotalRow() {
		return userAndDocumentTotalRow;
	}

	public void setUserAndDocumentTotalRow(String userAndDocumentTotalRow) {
		this.userAndDocumentTotalRow = userAndDocumentTotalRow;
	}

	public String getUserAndDocumentCurrentPage() {
		return userAndDocumentCurrentPage;
	}

	public void setUserAndDocumentCurrentPage(String userAndDocumentCurrentPage) {
		this.userAndDocumentCurrentPage = userAndDocumentCurrentPage;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public List<Document> getDocuments() {
		return documents;
	}

	public void setDocuments(List<Document> documents) {
		this.documents = documents;
	}

	public Pager getDocumentsPager() {
		return documentsPager;
	}

	public void setDocumentsPager(Pager documentsPager) {
		this.documentsPager = documentsPager;
	}

	public String getDocumentsMethod() {
		return documentsMethod;
	}

	public void setDocumentsMethod(String documentsMethod) {
		this.documentsMethod = documentsMethod;
	}

	public String getDocumentsTotalRow() {
		return documentsTotalRow;
	}

	public void setDocumentsTotalRow(String documentsTotalRow) {
		this.documentsTotalRow = documentsTotalRow;
	}

	public String getDocumentsCurrentPage() {
		return documentsCurrentPage;
	}

	public void setDocumentsCurrentPage(String documentsCurrentPage) {
		this.documentsCurrentPage = documentsCurrentPage;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getOrder_by_flag() {
		return order_by_flag;
	}

	public void setOrder_by_flag(String order_by_flag) {
		this.order_by_flag = order_by_flag;
	}

	public List<UserAndDocumentWrapper> getRankUserAndDocuementList() {
		return rankUserAndDocuementList;
	}

	public void setRankUserAndDocuementList(List<UserAndDocumentWrapper> rankUserAndDocuementList) {
		this.rankUserAndDocuementList = rankUserAndDocuementList;
	}

	public String getShow_label() {
		return show_label;
	}

	public void setShow_label(String show_label) {
		this.show_label = show_label;
	}

	public List<Map<String, String>> getOtherLinks() {
		return otherLinks;
	}

	public void setOtherLinks(List<Map<String, String>> otherLinks) {
		this.otherLinks = otherLinks;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
	
}
